package com.drawingtimer.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.drawingtimer.admin.converter.TProcessInfoConverter;
import com.drawingtimer.admin.enums.TProcessInfoEnum;
import com.drawingtimer.admin.model.entity.TProcessInfo;
import com.drawingtimer.admin.model.vo.TProcessInfoPageVO;
import com.drawingtimer.admin.model.qo.TProcessInfoQO;
import com.drawingtimer.admin.mapper.TProcessInfoMapper;
import com.drawingtimer.admin.service.TProcessInfoService;
import com.hccake.ballcat.common.model.domain.PageParam;
import com.hccake.ballcat.common.model.domain.PageResult;
import com.hccake.extend.mybatis.plus.conditions.query.LambdaQueryWrapperX;
import com.hccake.extend.mybatis.plus.service.impl.ExtendServiceImpl;
import com.hccake.extend.mybatis.plus.toolkit.WrappersX;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 工序信息
 *
 * @author jjh 2025-04-24 16:34:17
 */
@Service
public class TProcessInfoServiceImpl extends ExtendServiceImpl<TProcessInfoMapper, TProcessInfo>
		implements TProcessInfoService {

	/**
	 * 根据QueryObeject查询分页数据
	 * @param pageParam 分页参数
	 * @param qo 查询参数对象
	 * @return PageResult<TProcessInfoPageVO> 分页数据
	 */
	@Override
	public PageResult<TProcessInfoPageVO> queryPage(PageParam pageParam, TProcessInfoQO qo) {
		return baseMapper.queryPage(pageParam, qo);
	}

	@Override
	public PageResult<TProcessInfoPageVO> getDetailsByFlowCardNumber(String flowCardNumber) {
		return baseMapper.getDetailsByFlowCardNumber(flowCardNumber);
	}

	public PageResult<TProcessInfoPageVO> getReportingForWorkByFlowCardNumber(String flowCardNumber) {
		List<TProcessInfoPageVO> page = baseMapper.getReportingForWorkByFlowCardNumber(flowCardNumber);
		for (TProcessInfoPageVO vo : page) {
			if (vo.getEndTime() == null) {
				vo.setIsPaused(vo.getPauseStart() != null && vo.getPauseEnd() == null);
			}
			else {
				vo.setIsPaused(false);
			}
			TProcessInfoEnum status = TProcessInfoEnum.getByCode(vo.getStatus());
			if (status != null) {
				vo.setStatusMsg(status.getDesc());
			}
			if (vo.getSetupTime() != null && vo.getSetupTime().compareTo(BigDecimal.ZERO) > 0) {
				// 转换 1 为一小时 1.5 为 1.5小时 转换为 分钟
				vo.setSetupTime(vo.getSetupTime().multiply(new BigDecimal(60)));
			}
			if (vo.getStandardTime() != null && vo.getStandardTime().compareTo(BigDecimal.ZERO) > 0) {
				// 转换 1 为一小时 1.5 为 1.5小时 转换为 分钟
				vo.setStandardTime(vo.getStandardTime().multiply(new BigDecimal(60)));
			}
		}
		return new PageResult<>(page, page.size());
	}

	public boolean existsByKey(String flowCardNumber, Integer processSequence, String processCode) {
		LambdaQueryWrapper<TProcessInfo> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(TProcessInfo::getFlowCardNumber, flowCardNumber)
			.eq(TProcessInfo::getProcessSequence, processSequence)
			.eq(TProcessInfo::getProcessCode, processCode);
		return baseMapper.selectCount(queryWrapper) > 0;
	}

}
